package zhieasy.cn.xpu_helper.controller.common;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.Role;
import zhieasy.cn.model.common.WechatUserRole;
import zhieasy.cn.service.common.RoleService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author DaHang
 * @date 2020/8/13 23:16
 */
@Api(tags = "common模块，角色相关配置（目前无用）")
@RestController
@RequestMapping("/common/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    /**
     * 新增角色
     * @param role
     * @return
     */

    @RequestMapping(value = "addRole",method = RequestMethod.POST)
    public ResultVO addRole(@RequestBody Role role){
        boolean b = roleService.save(role);
        if(b){
            return ResultUtils.success("新增成功!");
        }else{
            return ResultUtils.error("新增失败！");
        }
    }

    /**
     * 根据id查询角色
     * @return
     */
    @RequestMapping(value = "/getRoleById",method = RequestMethod.POST)
    public ResultVO getRoleById(@RequestBody Role role){
        Role r = roleService.getById(role.getRole_id());
        return ResultUtils.success("成功",r);
    }

    /**
     * 编辑角色
     * @return
     */
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public ResultVO updateRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        if(b){
            return ResultUtils.success("编辑角色成功!");
        }else{
            return ResultUtils.error("编辑角色失败!");
        }
    }

    /**
     * 删除角色
     * @return
     */
    @RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
    public ResultVO deleteRole(@RequestBody Role role){


        boolean b = roleService.removeById(role.getRole_id());
        if(b){
            return ResultUtils.success("删除角色成功!");
        }else{
            return ResultUtils.error("删除角色失败!");
        }
    }

    /**
     * 分配角色时查询角色列表
     * @return
     */
    @RequestMapping(value = "/getRoleListForUser",method = RequestMethod.POST)
    public ResultVO getRoleListForUser(){
        List<Role> list = roleService.list();
        return ResultUtils.success("成功",list);
    }



    /**
     * 根据用户id查询角色id
     * @param session
     * @return
     */
    @RequestMapping(value = "/getRoleIdByUser",method = RequestMethod.POST)
    public ResultVO getRouleIdByUser(HttpSession session){
        WechatUserRole id = roleService.getRoleIdByWeChatUserOpenId((String) session.getAttribute("openId"));
        return ResultUtils.success("成功",id);
    }

    /**
     * 分配用户的角色
     * @param httpSession
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/assignRole",method = RequestMethod.POST)
    public ResultVO assignRole(HttpSession httpSession , Long roleId){
        roleService.assignRole((String) httpSession.getAttribute("openId"),roleId);
        return ResultUtils.success("分配成功!");
    }


}
